package com.dhcc.DRGsHosp.facade.impl;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.application.DrgsIndexCalApplication;
import com.dhcc.DRGsHosp.facade.DrgsIndexCalFacade;
import com.dhcc.DRGsHosp.facade.MonitorHistoryFacade;
import com.dhcc.DRGsHosp.facade.dto.IndexCalDTO;
import com.dhcc.DRGsHosp.facade.impl.assembler.IndexCalAssembler;
import com.dhcc.DRGsHosp.infra.util.Arith;
import com.dhcc.DRGsHosp.infra.util.DrgsIndex;

/**
 * @title		:MonitorHistoryFacadeImpl.java
 * @description	:
 * @author		:lileizheng
 * @date		:2016年8月9日
 * @version     :1.0
 * @copyright   :DHCC 2013-2014 
 */
@Named("drgsHospFacadeImpl33")
public class MonitorHistoryFacadeImpl implements MonitorHistoryFacade {
	@Autowired
	private DrgsIndexCalApplication drgsIndexCalApplication;
	
	@Autowired
	private DrgsIndexCalFacade drgsIndexCalFacade;
	

	/**
	 *  getQuotaList.  
	 *  @Description：获取指标计算值
	 *  @Author LiuHongjun
	 *  @param dto
	 *  @return 
	 *  @Date:2017年9月8日下午3:38:14
	 */
	private List<Map<String, Object>> getQuotaList(IndexCalDTO dto){
		String isHistory=dto.getIsHistory();
		List<Map<String, Object>> list = new LinkedList<>();
		if(!"1".equals(isHistory)){
			String stdId=drgsIndexCalFacade.getStdID(dto.getDateFrom(),dto.getDateTo());
			String verCode=drgsIndexCalFacade.getVerCode(dto.getDateFrom(),dto.getDateTo());
			String verRegion=drgsIndexCalFacade.getGroupScheme();
			if(stdId==null){
				return list;
			}else{
				dto.setStdId(stdId);
				dto.setVerCode(verCode);
				dto.setVerRegion(verRegion);
			}
		}
		String quotaCode=dto.getQuotaCode();
		//获取指标精度
	    String unit = drgsIndexCalApplication.getUnitByQuotaCode(quotaCode);
	    int unitn=Integer.parseInt(unit);
		List<Object[]> resultList = drgsIndexCalApplication.getDrgsIndex(IndexCalAssembler.toEntity(dto), quotaCode);
		for(Object[] objs: resultList){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ddesc", objs[0]);
			map.put("ccode", objs[1]);
			Double value = null;
			if(objs[2]!=null){
				value = Arith.div(Double.parseDouble(objs[2].toString()), 1,unitn);
				map.put(quotaCode, value);
			}
			list.add(map);
		}
		return list;
	}
/**
 * 
 *  @Description：获取历史分组器版本
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.MonitorHistoryFacade#getHisGroupVersion(com.dhcc.DRGsHosp.facade.dto.IndexCalDTO)
 */
	@Override
	public InvokeResult getHisGroupVersion(IndexCalDTO dto) {
		List<Map<String, Object>> list = drgsIndexCalApplication.getHisGroupVersion(dto);
		return InvokeResult.success(list);
	}
/**
 * 
 *  @Description：获取标杆年份
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.MonitorHistoryFacade#getStdYear()
 */
@Override
public InvokeResult getStdYear() {
	return InvokeResult.success(drgsIndexCalApplication.getStdYear());
}

@Override
public InvokeResult getStdType() {
	return InvokeResult.success(drgsIndexCalApplication.getStdType());
}
/**
 *  @Description：历史数据查询
 *  @Author LiuHongJun  
 *  @see com.dhcc.DRGsHosp.facade.MonitorHistoryFacade#findMonitorHistory(com.dhcc.DRGsHosp.facade.dto.IndexCalDTO)
 */
@Override
public InvokeResult findMonitorHistory(IndexCalDTO dto) {
	List<Map<String, Object>> list = new LinkedList<>();
	if(dto.getStdId()==null||"".equals(dto.getStdId())){
		dto.setStdId(drgsIndexCalFacade.getStdID(dto.getDateFrom(),dto.getDateTo()));
	}
	if(dto.getVerRegion()==null||"".equals(dto.getVerRegion())){
		dto.setVerRegion(drgsIndexCalFacade.getGroupScheme());
	}
	dto.setDateFrom(DrgsIndex.formateDateFrom(dto.getDateFrom()));//将时间格式化为 yyyy-mm-dd
    dto.setDateTo(DrgsIndex.formateDateTo(dto.getDateTo()));//将时间格式化为 yyyy-mm-dd
	//获取计算指标sql
	String sqlStr=drgsIndexCalApplication.getSqlByQuotaCode(dto.getQuotaCode());
	dto.setSqlStr(sqlStr);	
	try {
		List<Object[]> resultList = drgsIndexCalApplication.getDrgsIndex(IndexCalAssembler.toEntity(dto),dto.getQuotaCode());
		if("Base".equals(dto.getQuotaCode())){
			for(Object[] objs: resultList){
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("ddesc", objs[0]);
				map.put("ccode", objs[1]);
				list.add(map);
			}
		}else{
			list = getQuotaList(dto);
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return InvokeResult.success(list);

}
}
